package com.example.demo.scheduler;

import com.example.demo.service.SubscriptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SubscriptionScheduler {
    private static final Logger log = LoggerFactory.getLogger(SubscriptionScheduler.class);

    private final SubscriptionService subscriptionService;

    public SubscriptionScheduler(SubscriptionService subscriptionService) {
        this.subscriptionService = subscriptionService;
    }

    // 每天凌晨1点执行清理过期订阅
    @Scheduled(cron = "0 0 1 * * ?")
    public void cleanupExpiredSubscriptions() {
        log.info("开始执行订阅过期清理任务");
        try {
            subscriptionService.cleanupExpiredSubscriptions();
            log.info("订阅过期清理任务完成");
        } catch (Exception e) {
            log.error("订阅过期清理任务执行失败", e);
        }
    }
}